package org.nlp2rdf.navigator.component.learn;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import org.aksw.commons.jena.SaveOntology;
import org.apache.log4j.Logger;
import org.dllearner.core.KnowledgeSource;
import org.dllearner.core.configurators.ComponentFactory;
import org.dllearner.kb.OWLFile;
import org.dllearner.utilities.URLencodeUTF8;
import org.dllearner.utilities.experiments.Examples;
import org.nlp2rdf.navigator.component.Component;
import org.nlp2rdf.navigator.component.exception.ComponentInitException;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;

public class KsFromMaterializedFiles extends Component implements IKnowledgeSources {
	private static final Logger logger = Logger.getLogger(KsFromMaterializedFiles.class);

	private String exampleSourceDir = null;
	private Set<String> schemaFiles =  new HashSet<String>();
	private Set<URL> schemaURLs = new HashSet<URL>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nlp2rdf.navigator.component.IComponent#init()
	 */
	@Override
	protected void _init() throws ComponentInitException {
		try {
			exampleSourceDir = componentConfig.getPathModifier() + exampleSourceDir;
			for (String filename : schemaFiles) {
				schemaURLs.add(new File(componentConfig.getPathModifier() + filename).toURI().toURL());
			}
		} catch (Exception e) {
			String message = "error in KsFromMaterializedFiles " + exampleSourceDir + " " + schemaURLs;
			logger.error(message, e);
			throw new ComponentInitException(message, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.nlp2rdf.navigator.component.learn.IKnowledgeSources#getKnowledgeSources
	 * (org.dllearner.utilities.experiments.Examples)
	 */
	public Set<KnowledgeSource> getKnowledgeSources(Examples ex) {
		check();
		Set<KnowledgeSource> knowledgeSources = new HashSet<KnowledgeSource>();
		List<URL> urls = new ArrayList<URL>();
		// note can be simplified after dl-Learner is updated
		urls.addAll(exampleToFileURLs(ex.getPositiveExamples()));
		urls.addAll(exampleToFileURLs(ex.getNegativeExamples()));

		urls.addAll(schemaURLs);

		for (URL u : urls) {
			logger.debug("adding knowledge source: " + u.toString());
			OWLFile ks = ComponentFactory.getOWLFile(u);
			knowledgeSources.add(ks);
		}
		return knowledgeSources;
	}

	/**
	 * this method relies on toURL, because theres seems to be a java error
	 * @param exampleURIs
	 * @return
	 */
	private List<URL> exampleToFileURLs(Set<String> exampleURIs) {
		List<URL> u = new ArrayList<URL>();
		for (String exampleURI : exampleURIs) {
			try {
				String encoded = URLencodeUTF8.encode(exampleURI);
				File f = new File(exampleSourceDir +encoded);
                Model m =  ModelFactory.createDefaultModel();
                m.read(f.toURI().toURL().toString());
                m.getResource("http://nlp2rdf.org/sso").removeProperties();
                SaveOntology.saveAsRDFXML(m,exampleSourceDir +encoded);

//				u.add(f.toURL());
				u.add(f.toURI().toURL());
			} catch (MalformedURLException e) {
				logger.error("skipping invalid URL " + componentConfig.getPathModifier()
						+ URLencodeUTF8.encode(exampleURI));
			}
		}
		return u;
	}

	public void setSchemaFiles(Set<String> schemaFiles) {
		this.schemaFiles = schemaFiles;
	}

	public void setExampleSourceDir(String exampleSourceDir) {
		this.exampleSourceDir = exampleSourceDir;
	}

}
